<template>
	<el-dialog v-model="showModal" class="peak-dialog" :close-on-click-modal="false" :title="title" :width="900">
		<el-form ref="paramsFormRef" class="params-form" :inline="true" label-position="right" label-width="100px" :model="paramsForm" :rules="paramsRules">
			<el-row :gutter="16" justify="start">
				<el-col :span="12">
					<el-form-item class="form-item" label="模型标识:" prop="modelKey">
						<el-input v-model="paramsForm.modelKey" :disabled="Boolean(paramsForm.modelId)" placeholder="请输入模型标识" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="模型类型:" prop="formType">
						<el-select v-model="paramsForm.formType" :disabled="Boolean(paramsForm.modelId)" class="el-form-item" placeholder="请选择模型类型">
							<el-option label="静态" value="1" />
							<el-option label="动态" value="2" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="模型名称:" prop="modelName">
						<el-input v-model="paramsForm.modelName" placeholder="请输入模型名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="paramsForm.formType == '2'">
					<el-form-item class="form-item" label="流程分类:" prop="category">
						<dict-select v-model="paramsForm.category" :dictKey="'model_category'" placeholder="请选择流程分类" />
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="paramsForm.formType == '1'">
					<el-form-item class="form-item" label="详情路径:" prop="detailsPath">
						<el-input v-model="paramsForm.detailsPath" placeholder="请输入详情路径" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="form-item" label="备注信息:" prop="description">
						<el-input v-model="paramsForm.description" rows="3" type="textarea" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleCancel">取 消</el-button>
				<el-button type="primary" @click="handleSure" v-throttle>确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup>
import global from '@/utils/global'
const emit = defineEmits(['peak-modal-ok'])
/*
 *@Description: 初始化数据
 *@Author: liuYuFeng
 *@Date: 2023-02-24 16:33:25
 */
const state = reactive({
	showModal: false,
	paramsFormRef: null,
	title: '',
	paramsForm: {
		modelId: '',
		modelName: '',
		modelKey: '',
		category: '',
		description: '',
		formType: '2',
	},
	paramsRules: {
		modelName: [{ required: true, message: '请输入模型名称', trigger: 'blur' }],
		modelKey: [{ required: true, message: '请输入模型标识', trigger: 'blur' }],
		category: [{ required: true, message: '请选择流程分类', trigger: 'blur' }],
	},
})
const { showModal, title, paramsForm, paramsRules, paramsFormRef } = toRefs(state)

/*
 *@Description: 方法method
 *@Author: liuYuFeng
 *@Date: 2023-02-24 15:34:16
 */
const handleSure = () => {
	state.paramsFormRef.validate(valid => {
		if (valid) {
			handleSureOk()
		} else {
			return false
		}
	})
}
const handleSureOk = () => {
	if (state.paramsForm.modelId) {
		//编辑
		global.$fetchPostData(`/sop-joint-disposal/web/v1/model/edit`, state.paramsForm).then(data => {
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	} else {
		delete state.paramsForm.configId
		global.$fetchPostData(`/sop-joint-disposal/web/v1/model/add`, state.paramsForm).then(data => {
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 1)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	}
}
const handleCancel = () => {
	state.showModal = false
}
const handleShowModal = async row => {
	state.showModal = true
	await nextTick()
	state.paramsForm.modelId = ''
	if (row) {
		state.title = '流程模型编辑'
		state.paramsFormRef.resetFields()
		getDataDetail(row.modelId)
	} else {
		state.title = '新增流程模型'
		state.paramsFormRef.resetFields()
	}
}
const getDataDetail = modelId => {
	global.$fetchGetData(`/sop-joint-disposal/web/v1/model/details`, { modelId: modelId }).then(data => {
		if (data.code == 200) {
			state.paramsForm = data.data || {}
		} else {
			global.$baseMessage(data.msg, 'error')
		}
	})
}
defineExpose({
	handleShowModal,
})
</script>
<style lang="scss" scoped></style>
